{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "'''\n",
    "shell 命令\n",
    "将flv转mp4\n",
    "'''\n",
    "\n",
    "\n",
    "# shell 命令\n",
    "\n",
    "import os, re\n",
    "# path = '/Users/earlshaw/Downloads'\n",
    "# path = '/Users/earlshaw/Desktop/test'\n",
    "\n",
    "path = '/Volumes/public/tutorial_video/张飞/硬件设计与开发第07部'\n",
    "\n",
    "os.chdir(path)\n",
    "\n",
    "# flv_files = []\n",
    "for f in os.listdir('.'):\n",
    "    if re.match(r'.*?flv', f):\n",
    "        f_a = f.split('.')[0]+'.mp4' #用\n",
    "        os.system(f\"ffmpeg -i {f} -c copy {f_a}\")\n",
    "#         flv_files.append(f)\n",
    "# flv_files.sort(key=str.lower)\n",
    "\n",
    "\n",
    "\n",
    "# os.system(\"ffmpeg -i 'y_zj_4_lesson6_recv.flv' -c copy 'output.mp4'\")\n",
    "# r_code = os.system('ls -al')\n",
    "# print('return code:', r_code)\n",
    "# print(os.getcwd())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将文件名中的.改为_\n",
    "import os, re, shutil\n",
    "# path = '/Users/earlshaw/Downloads'\n",
    "# path = '/Users/earlshaw/Desktop/test'\n",
    "path = '/Users/earlshaw/Desktop/python_video'\n",
    "os.chdir(path)\n",
    "\n",
    "a_files = []\n",
    "for f in os.listdir('.'):\n",
    "    if re.match(r'.*?avi', f):\n",
    "        # f_a = f.split('.')[0]+'.mp4' #用\n",
    "        # a_files.append(f)\n",
    "        f_1 = re.sub('\\.', '_', f[:-4]) # .表示除换行符之外的任何字符，\\.表示.\n",
    "        shutil.move(f, f_1+'.avi')\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "特殊视频\n",
    "提取.avi中的音频，视频\n",
    "将视频转码\n",
    "打包入mp4\n",
    "'''\n",
    "\n",
    "# shell 命令\n",
    "\n",
    "import os, re\n",
    "# path = '/Users/earlshaw/Downloads'\n",
    "path = '/Users/earlshaw/Desktop/test'\n",
    "os.chdir(path)\n",
    "\n",
    "a_files = []\n",
    "for f in os.listdir('.'):\n",
    "    if re.match(r'.*?avi', f):\n",
    "        a_files.append(f)\n",
    "a_files.sort()\n",
    "a_files\n",
    "        \n",
    "for f in a_files:\n",
    "    os.system(f\"ffmpeg -i '{f}' -vn -acodec copy aa.wav\")\n",
    "    os.system(f\"ffmpeg -i aa.wav -acodec aac aa.aac\")\n",
    "    os.remove('aa.wav')\n",
    "    os.system(f\"ffmpeg -i '{f}' -c:v copy -an aa.mp4\")\n",
    "    os.remove(f)\n",
    "    os.system(f\"ffmpeg -i aa.mp4 -i aa.aac -c copy -map 0:v:0 -map 1:a:0 '{f.split('.')[0]+'.mp4'}'\")\n",
    "    os.remove('aa.aac')\n",
    "    os.remove('aa.mp4')\n",
    "    print(f'{f} done')\n",
    "\n",
    "\n",
    "\n",
    "        # os.system(f\"ffmpeg -i '{f}' -vn -acodec copy '{f.split('.')[0]+'.wav'}'\")\n",
    "        # os.system(f\"ffmpeg -i '{f.split('.')[0]+'.wav'}' -acodec aac  '{f.split('.')[0]+'.aac'}'\")\n",
    "        # os.system(f\"ffmpeg -i '{f}' -c:v copy -an '{f.split('.')[0]+'.mp4'}'\")\n",
    "        # os.system(f\"ffmpeg -i '{f.split('.')[0]+'.mp4'}' -i '{f.split('.')[0]+'.aac'}' -c copy -map 0:v:0 -map 1:a:0 '{f.split('.')[0]+'2.mp4'}'\")\n",
    "        # # os.remove(f)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['051701410 #主被动按钮座# C.pdf', '051701582 #主被动焊接螺母# B.pdf', '051701636 #主被动连接轴# B.pdf', '051701719 #主被动下连杆# A.pdf', '051701720 #主被动上连杆# A.pdf', '051701721 #主被动上连杆# A.pdf', '051701722 #主被动轴套# A.pdf', '051701723 #主被动压紧块# A.pdf', '051701724 #主被动调节螺杆# A.pdf', '051701725 #主被动盖板# A.pdf', '051702074 #主被动上肢架# C.pdf', '051702075 #主被动上肢移动臂# C.pdf', '051702078 #主被动纵梁# C.pdf', '051702079_1 #主被动安装板# B.pdf', '051702079_2 #主被动安装板# B.pdf', '051702081 #主被动安装底板# C.pdf', '051702082 #主被动脚垫架# C.pdf', '051702083 #主被动减速器座# C.pdf', '051702097 #主被动脚轮架# C.pdf', '051702098 #主被动脚轮架# C.pdf', '051702114 #主被动减速器架# C.pdf', '051702118 #主被动横梁# C.pdf', '051702119 #主被动电器架# B.pdf', '051702123 #主被动减速器架# C.pdf', '051702153 #主被动导轨架# C.pdf', '051702285 #主被动传感器架# B.pdf', '051702286 #主被动轴承盖# B.pdf', '051702287 #主被动电机座# B.pdf', '051702288 #主被动立柱外壳架# B.pdf'] 29\n",
      "['aa', '051701410 #主被动按钮座# C.pdf', '051701582 #主被动焊接螺母# B.pdf', '051701636 #主被动连接轴# B.pdf', '051701719 #主被动下连杆# A.pdf', '051701720 #主被动上连杆# A.pdf', '051701721 #主被动上连杆# A.pdf', '051701722 #主被动轴套# A.pdf', '051701723 #主被动压紧块# A.pdf', '051701724 #主被动调节螺杆# A.pdf', '051701725 #主被动盖板# A.pdf', '051702074 #主被动上肢架# C.pdf', '051702075 #主被动上肢移动臂# C.pdf', '051702078 #主被动纵梁# C.pdf', '051702079_1 #主被动安装板# B.pdf', '051702079_2 #主被动安装板# B.pdf', '051702081 #主被动安装底板# C.pdf', '051702082 #主被动脚垫架# C.pdf', '051702083 #主被动减速器座# C.pdf', '051702097 #主被动脚轮架# C.pdf', '051702098 #主被动脚轮架# C.pdf', '051702114 #主被动减速器架# C.pdf', '051702118 #主被动横梁# C.pdf', '051702119 #主被动电器架# B.pdf', '051702123 #主被动减速器架# C.pdf', '051702153 #主被动导轨架# C.pdf', '051702285 #主被动传感器架# B.pdf', '051702286 #主被动轴承盖# B.pdf', '051702287 #主被动电机座# B.pdf'] 29\n",
      "done\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "文件名依次向前错位\n",
    "批量重命名纠正\n",
    "'''\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "import shutil\n",
    "for root, dirs, files in os.walk(dirpath):\n",
    "#     print(files, len(files))\n",
    "    files_n = files.copy() #源文件名list复制到目标文件名list\n",
    "    files_n.insert(0, 'aa') #目标文件名list元素后移\n",
    "#     print(files, len(files))\n",
    "    del files_n[-1] #删除最后一项，源文件名list与目标文件名list元素相等\n",
    "    print(files, len(files))\n",
    "    print(files_n, len(files_n))\n",
    "    i = 0\n",
    "    for file in files:\n",
    "        shutil.move(os.path.join(root, file), os.path.join(root, files_n[i])) #源文件名依次替换目标文件名\n",
    "        i = i+1\n",
    "    print('done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "漫画重命名\n",
    "使用正则，将文件名拆分，重组\n",
    "'''\n",
    "\n",
    "import shutil, os, re, logging\n",
    "logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')\n",
    "logging.disable(logging.CRITICAL)\n",
    "os.chdir('g:/教头当家 - Copy')\n",
    "\n",
    "txt_p = re.compile(r'''^(\\d{3})_          # 开头3位数字\n",
    "                    ((0|1|2)?\\d)卷        # 中间2位数字，第一位为0或1或2或3\n",
    "                    (.*?)$                 # 结尾\n",
    "                    ''',re.VERBOSE) # re.VERBOSE，将多行正则的#后为注释\n",
    "  #后续正则的分段，只取括号内的内容，如第二段，没有'卷'字\n",
    "logging.debug(os.listdir('.'))\n",
    "for f_name in os.listdir('.'): #返回文件夹内文件列表\n",
    "    mo = txt_p.search(f_name) \n",
    "    logging.debug(mo)\n",
    "    logging.debug(mo.group(1))\n",
    "    logging.debug(mo.group(2))\n",
    "    logging.debug(mo.group(3)) #为什么mo.group(3)=0\n",
    "    logging.debug(mo.group(4))\n",
    "    if mo == None: \n",
    "        continue\n",
    "    pre_p = '教头当家_vol' # 文件名前缀\n",
    "    m_p = mo.group(2)\n",
    "    sub_p = mo.group(4)\n",
    "    f_new = f'{pre_p}{m_p}{sub_p}' # 重组文件名\n",
    "    abs_dir = os.path.abspath('.')\n",
    "    amer_f = os.path.join(abs_dir, f_name)\n",
    "    euro_f = os.path.join(abs_dir, f_new)\n",
    "    logging.debug(f'rename {amer_f} to {euro_f}...')\n",
    "    shutil.move(amer_f, euro_f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['y_zj_6_lesson10_recv.mp4',\n",
       " 'y_zj_6_lesson11_recv.mp4',\n",
       " 'y_zj_6_lesson12_recv.mp4',\n",
       " 'y_zj_6_lesson13_recv.mp4',\n",
       " 'y_zj_6_lesson14_recv.mp4',\n",
       " 'y_zj_6_lesson15_a_recv.mp4',\n",
       " 'y_zj_6_lesson15_b_recv.mp4',\n",
       " 'y_zj_6_lesson16_recv.mp4',\n",
       " 'y_zj_6_lesson17_a_recv.mp4',\n",
       " 'y_zj_6_lesson17_b_recv.mp4',\n",
       " 'y_zj_6_lesson18_recv.mp4',\n",
       " 'y_zj_6_lesson19_a_recv.mp4',\n",
       " 'y_zj_6_lesson19_b_recv.mp4',\n",
       " 'y_zj_6_lesson19_c_recv.mp4',\n",
       " 'y_zj_6_lesson3_recv.mp4',\n",
       " 'y_zj_6_lesson4_recv.mp4',\n",
       " 'y_zj_6_lesson7_recv.mp4',\n",
       " 'y_zj_6_lesson9_recv.mp4']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 批量重命名，失败\n",
    "\n",
    "import re, os, sys\n",
    "# path = 'D:/rename'\n",
    "path = '/Users/earlshaw/Desktop/test'\n",
    "# path = '/Volumes/public/aa/张飞/硬件设计与开发第05部'\n",
    "\n",
    "os.chdir(path)\n",
    "# os.getcwd()\n",
    "\n",
    "if not os.path.exists('./ax.txt'):\n",
    "    print('no name file!')\n",
    "    sys.exit()\n",
    "    # exit\n",
    "# break\n",
    "\n",
    "\n",
    "\n",
    "# 将重命名的目标字符串，读取为列表\n",
    "with open('ax.txt', 'r', encoding='utf-8') as f:\n",
    "    a_l = list(f)\n",
    "    b_l = []\n",
    "    i = 1\n",
    "    for a in a_l:\n",
    "        if i < 10:\n",
    "            b = f'0{i}_'+re.sub('\\n', '', a)+'.mp4' # 删除不想要字符，增加后缀名，增加编号\n",
    "        else:\n",
    "            b = f'{i}_'+re.sub('\\n', '', a)+'.mp4'\n",
    "        # print(b)\n",
    "        b_l.append(b)\n",
    "        i = i+1\n",
    "b_l\n",
    "\n",
    "# 将待重命名文件，读取为列表\n",
    "file_l = os.listdir(path)\n",
    "file_lb = []\n",
    "for f in file_l:\n",
    "    if re.match('.*mp4', f) != None: # 根据后缀过滤\n",
    "        file_lb.append(f)\n",
    "\n",
    "for fb in file_lb: # 此行继续用f会有bug，某些元素处理不上\n",
    "    if fb[14] == '_': # 如果lesson后为1位，则15位为'_'\n",
    "        # print(fb)\n",
    "        # fa = fb[:13]+ '0' +fb[13:]\n",
    "        # print(fa)\n",
    "        file_lb.remove(fb)\n",
    "        # file_lb.append(fa)\n",
    "        # print(fa)\n",
    "# for f in file_lb:\n",
    "#     print(len(f))\n",
    "file_lb.sort()\n",
    "file_lb\n",
    "# print(file_lb[13][14])\n",
    "\n",
    "\n",
    "# # 重命名文件\n",
    "# i = 0\n",
    "# for f in file_lb:\n",
    "#     if len(b_l) != len(file_lb):\n",
    "#         print(\"the name list doesn't match!\")\n",
    "#         break\n",
    "#     old_f = os.path.join(path, f)\n",
    "#     print(old_f)\n",
    "#     new_f = os.path.join(path, b_l[i])\n",
    "#     print(new_f)\n",
    "#     os.rename(old_f, new_f)\n",
    "#     print(f'{old_f} has renamed as {new_f}')\n",
    "#     i = i+1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将 .nes后缀名移入另一文件夹\n",
    "\n",
    "# 批量重命名\n",
    "import re, os, sys\n",
    "# path = 'D:/rename'\n",
    "path = '/Users/earlshaw/Desktop/fc/fc roms/fc'\n",
    "# path = '/Volumes/public/aa/张飞/硬件设计与开发第05部'\n",
    "\n",
    "os.chdir(path)\n",
    "# os.getcwd()\n",
    "\n",
    "for root,dirs,files in os.walk(path):\n",
    "    print(files)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 修改后缀名\n",
    "import os, re\n",
    "\n",
    "path = '/Volumes/Untitled/魔神英雄传1080P修复'\n",
    "os.chdir(path)\n",
    "\n",
    "# 列出文件夹内所有pdf，x_t并排序\n",
    "\n",
    "pdf_files = []\n",
    "\n",
    "for f in os.listdir('.'):\n",
    "    if re.match(r'.*mp41', f):\n",
    "        pdf_files.append(f)\n",
    "pdf_files.sort(key=str.lower)\n",
    "print(pdf_files)\n",
    "print(len(pdf_files))\n",
    "\n",
    "for f_name in pdf_files:\n",
    "    root, ext = os.path.splitext(f_name)\n",
    "    new_file_name = root + \".mp4\"\n",
    "    os.rename(f_name, new_file_name)\n",
    "\n",
    "print('done!')\n",
    "\n",
    "\n",
    "# file_name = \"example.txt\"\n",
    "# root, ext = os.path.splitext(file_name)\n",
    "# new_file_name = root + \".csv\"\n",
    "# os.rename(file_name, new_file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Archive:  Chaos Seed - Fuusui Kairouki (Japan).zip\n",
      "100 done!\n",
      "200 done!\n",
      "300 done!\n",
      "400 done!\n"
     ]
    }
   ],
   "source": [
    "# 3000+zip解压缩\n",
    "\n",
    "import os, re\n",
    "\n",
    "# path = '/Users/earlshaw/Downloads/md_rom'\n",
    "path = '/Users/earlshaw/Downloads/sfc_rom'\n",
    "os.chdir(path)\n",
    "\n",
    "# 列出文件夹内所有pdf，x_t并排序\n",
    "\n",
    "# a_files = []\n",
    "\n",
    "i = 0\n",
    "\n",
    "for f in os.listdir('.'):\n",
    "    if re.match(r'.*zip', f):\n",
    "        os.system(f\"unzip \\\"{f}\\\" -d .\") #文件名有空格，需要引号\n",
    "        i = i+1\n",
    "    if i % 100 == 0:\n",
    "        print(f'{i} done!')\n",
    "\n",
    "# a_files.sort(key=str.lower)\n",
    "# print(a_files)\n",
    "# print(len(a_files))\n",
    "\n",
    "# for f in os.listdir('.'):\n",
    "#     if re.match(r'.*?flv', f):\n",
    "#         f_a = f.split('.')[0]+'.mp4' #用\n",
    "#         os.system(f\"ffmpeg -i {f} -c copy {f_a}\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 移动目录下含(1).mp4的文件\n",
    "\n",
    "import os, re, shutil\n",
    "\n",
    "# path = '/Users/earlshaw/Downloads/md_rom'\n",
    "path = 'D:/vitrual_share/直播课32个python项目'\n",
    "os.chdir(path)\n",
    "\n",
    "for root, dirs, files in os.walk(path):\n",
    "    # print(root) # root是目录下的所有文件夹\n",
    "    # print(dirs) # 将层级下的文件夹存为列表\n",
    "    for f in files:\n",
    "        if re.match(r'.*\\(1\\).mp4', f):\n",
    "            a = root + os.sep + f # os.sep是目前系统的路径分隔符\n",
    "                # a返回f的绝对路径\n",
    "            # print(a)\n",
    "            shutil.move(a, '.')\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m/Users/earlshaw/Nutstore Files/study_pro/python_code/自撸代码/操作文件.ipynb Cell 11\u001b[0m line \u001b[0;36m2\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X21sZmlsZQ%3D%3D?line=20'>21</a>\u001b[0m \u001b[39mif\u001b[39;00m re\u001b[39m.\u001b[39mmatch(re_part, f):\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X21sZmlsZQ%3D%3D?line=21'>22</a>\u001b[0m     \u001b[39mif\u001b[39;00m os\u001b[39m.\u001b[39mpath\u001b[39m.\u001b[39mexists(f):\n\u001b[0;32m---> <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X21sZmlsZQ%3D%3D?line=22'>23</a>\u001b[0m         b \u001b[39m=\u001b[39m f\u001b[39m.\u001b[39msplit()[\u001b[39m0\u001b[39m]\u001b[39m+\u001b[39m\u001b[39mf\u001b[39m\u001b[39m'\u001b[39m\u001b[39m(\u001b[39m\u001b[39m{\u001b[39;00mi\u001b[39m}\u001b[39;00m\u001b[39m)\u001b[39m\u001b[39m'\u001b[39m\u001b[39m+\u001b[39mf\u001b[39m.\u001b[39;49msplit()[\u001b[39m1\u001b[39;49m]\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X21sZmlsZQ%3D%3D?line=23'>24</a>\u001b[0m         shutil\u001b[39m.\u001b[39mmove(f,b) \u001b[39m# 将已有文件重命名\u001b[39;00m\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X21sZmlsZQ%3D%3D?line=24'>25</a>\u001b[0m     \u001b[39melse\u001b[39;00m:\n",
      "\u001b[0;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "# 移动目录下含zip的文件\n",
    "\n",
    "import os, re, shutil\n",
    "\n",
    "# path = '/Users/earlshaw/Downloads/md_rom'\n",
    "path = '/Users/earlshaw/Downloads/gba'\n",
    "re_part = r'.*zip'\n",
    "\n",
    "# path = '/Users/earlshaw/Downloads/fc'\n",
    "# re_part = r'.*zip'\n",
    "\n",
    "\n",
    "os.chdir(path)\n",
    "\n",
    "i = 0\n",
    "\n",
    "for root, dirs, files in os.walk(path):\n",
    "    # print(root) # root是目录下的所有文件夹\n",
    "    # print(dirs) # 将层级下的文件夹存为列表\n",
    "    for f in files:\n",
    "        if re.match(re_part, f):\n",
    "            a = root + os.sep + f # os.sep是目前系统的路径分隔符\n",
    "                # a返回f的绝对路径\n",
    "            # print(a)\n",
    "            shutil.move(a, '.') # 重复的文件会报错，怎么办\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 移动目录下含zip的文件\n",
    "\n",
    "import os, re, shutil\n",
    "\n",
    "# path = '/Users/earlshaw/Downloads/md_rom'\n",
    "# path = '/Users/earlshaw/Downloads/gba'\n",
    "# re_part = r'.*zip'\n",
    "\n",
    "# path = '/Users/earlshaw/Downloads/fc'\n",
    "# re_part = r'.*zip'\n",
    "\n",
    "# path = '/Users/earlshaw/Downloads/fc'\n",
    "# re_part = r'.*(nes)|(NES)'\n",
    "\n",
    "path = '/Users/earlshaw/Downloads/fc'\n",
    "re_part = r'.*nes' # 忽略大小写怎么做?\n",
    "\n",
    "\n",
    "\n",
    "os.chdir(path)\n",
    "\n",
    "# i = 0\n",
    "\n",
    "for root, dirs, files in os.walk(path):\n",
    "    # print(root) # root是目录下的所有文件夹\n",
    "    # print(dirs) # 将层级下的文件夹存为列表\n",
    "    for f in files:\n",
    "        if re.match(re_part, f):\n",
    "            if os.path.exists(f):\n",
    "                continue\n",
    "                # b = f.split()[0]+f'({i})'+f.split()[1]\n",
    "                # shutil.move(f,b) # 将已有文件重命名\n",
    "            else:\n",
    "                a = root + os.sep + f # os.sep是目前系统的路径分隔符\n",
    "                    # a返回f的绝对路径\n",
    "                # print(a)\n",
    "                shutil.move(a, '.') # 重复的文件会报错，怎么办\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 移动.mp4的文件\n",
    "\n",
    "import os, re, shutil\n",
    "\n",
    "# path = '/Users/earlshaw/Downloads/md_rom'\n",
    "path = 'D:/vitrual_share/直播课32个python项目'\n",
    "os.chdir(path)\n",
    "\n",
    "for root, dirs, files in os.walk(path):\n",
    "    # print(root) # root是目录下的所有文件夹\n",
    "    # print(dirs) # 将层级下的文件夹存为列表\n",
    "    for f in files:\n",
    "        if re.match(r'.*.mp4', f):\n",
    "            a = root + os.sep + f # os.sep是目前系统的路径分隔符\n",
    "                # a返回f的绝对路径\n",
    "            # print(a)\n",
    "            shutil.move(a, '.')\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'D:/vitrual_share/python_70'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m/Users/earlshaw/Nutstore Files/study_pro/python_code/自撸代码/操作文件.ipynb Cell 14\u001b[0m line \u001b[0;36m7\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X14sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m \u001b[39m# path = '/Users/earlshaw/Downloads/md_rom'\u001b[39;00m\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X14sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m path \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39mD:/vitrual_share/python_70\u001b[39m\u001b[39m'\u001b[39m\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X14sZmlsZQ%3D%3D?line=6'>7</a>\u001b[0m os\u001b[39m.\u001b[39;49mchdir(path)\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X14sZmlsZQ%3D%3D?line=8'>9</a>\u001b[0m \u001b[39mfor\u001b[39;00m root, dirs, files \u001b[39min\u001b[39;00m os\u001b[39m.\u001b[39mwalk(path):\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X14sZmlsZQ%3D%3D?line=9'>10</a>\u001b[0m     \u001b[39m# print(root) # root是目录下的所有文件夹\u001b[39;00m\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X14sZmlsZQ%3D%3D?line=10'>11</a>\u001b[0m     \u001b[39m# print(dirs) # 将层级下的文件夹存为列表\u001b[39;00m\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X14sZmlsZQ%3D%3D?line=11'>12</a>\u001b[0m     \u001b[39mfor\u001b[39;00m f \u001b[39min\u001b[39;00m files:\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'D:/vitrual_share/python_70'"
     ]
    }
   ],
   "source": [
    "# 查询.mp4文件位置\n",
    "\n",
    "import os, re, shutil\n",
    "\n",
    "# path = '/Users/earlshaw/Downloads/md_rom'\n",
    "path = 'D:/vitrual_share/python_70'\n",
    "os.chdir(path)\n",
    "\n",
    "for root, dirs, files in os.walk(path):\n",
    "    # print(root) # root是目录下的所有文件夹\n",
    "    # print(dirs) # 将层级下的文件夹存为列表\n",
    "    for f in files:\n",
    "        if re.match(r'.*.mp4', f):\n",
    "            a = root + os.sep + f # os.sep是目前系统的路径分隔符\n",
    "                # a返回f的绝对路径\n",
    "            print(a)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": true,
    "editable": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lesson_5-1.mp4\n"
     ]
    },
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'y_zj_5_lesson01_recv.mp4'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "File \u001b[0;32m~/anaconda3/envs/pytorch/lib/python3.10/shutil.py:816\u001b[0m, in \u001b[0;36mmove\u001b[0;34m(src, dst, copy_function)\u001b[0m\n\u001b[1;32m    815\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m--> 816\u001b[0m     os\u001b[39m.\u001b[39;49mrename(src, real_dst)\n\u001b[1;32m    817\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mOSError\u001b[39;00m:\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'y_zj_5_lesson01_recv.mp4' -> 'lesson_5-1.mp4'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m/Users/earlshaw/Nutstore Files/study_pro/python_code/自撸代码/操作文件.ipynb Cell 13\u001b[0m line \u001b[0;36m3\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X16sZmlsZQ%3D%3D?line=36'>37</a>\u001b[0m         a \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39mlesson\u001b[39m\u001b[39m'\u001b[39m\u001b[39m+\u001b[39mcha\u001b[39m+\u001b[39m\u001b[39mstr\u001b[39m(b)\u001b[39m+\u001b[39m\u001b[39m'\u001b[39m\u001b[39m.mp4\u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X16sZmlsZQ%3D%3D?line=37'>38</a>\u001b[0m         \u001b[39mprint\u001b[39m(a)\n\u001b[0;32m---> <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X16sZmlsZQ%3D%3D?line=38'>39</a>\u001b[0m         shutil\u001b[39m.\u001b[39;49mmove(f,a)\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X16sZmlsZQ%3D%3D?line=40'>41</a>\u001b[0m \u001b[39m# for f in mp4_files:\u001b[39;00m\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X16sZmlsZQ%3D%3D?line=41'>42</a>\u001b[0m \u001b[39m#     if f[14] == '_':\u001b[39;00m\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X16sZmlsZQ%3D%3D?line=42'>43</a>\u001b[0m \u001b[39m#         # print(f[13])\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X16sZmlsZQ%3D%3D?line=49'>50</a>\u001b[0m \u001b[39m#         a = 'lesson'+cha+f[13:15]+'.mp4'\u001b[39;00m\n\u001b[1;32m     <a href='vscode-notebook-cell:/Users/earlshaw/Nutstore%20Files/study_pro/python_code/%E8%87%AA%E6%92%B8%E4%BB%A3%E7%A0%81/%E6%93%8D%E4%BD%9C%E6%96%87%E4%BB%B6.ipynb#X16sZmlsZQ%3D%3D?line=50'>51</a>\u001b[0m \u001b[39m#         shutil.move(f,a)\u001b[39;00m\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch/lib/python3.10/shutil.py:836\u001b[0m, in \u001b[0;36mmove\u001b[0;34m(src, dst, copy_function)\u001b[0m\n\u001b[1;32m    834\u001b[0m         rmtree(src)\n\u001b[1;32m    835\u001b[0m     \u001b[39melse\u001b[39;00m:\n\u001b[0;32m--> 836\u001b[0m         copy_function(src, real_dst)\n\u001b[1;32m    837\u001b[0m         os\u001b[39m.\u001b[39munlink(src)\n\u001b[1;32m    838\u001b[0m \u001b[39mreturn\u001b[39;00m real_dst\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch/lib/python3.10/shutil.py:434\u001b[0m, in \u001b[0;36mcopy2\u001b[0;34m(src, dst, follow_symlinks)\u001b[0m\n\u001b[1;32m    432\u001b[0m \u001b[39mif\u001b[39;00m os\u001b[39m.\u001b[39mpath\u001b[39m.\u001b[39misdir(dst):\n\u001b[1;32m    433\u001b[0m     dst \u001b[39m=\u001b[39m os\u001b[39m.\u001b[39mpath\u001b[39m.\u001b[39mjoin(dst, os\u001b[39m.\u001b[39mpath\u001b[39m.\u001b[39mbasename(src))\n\u001b[0;32m--> 434\u001b[0m copyfile(src, dst, follow_symlinks\u001b[39m=\u001b[39;49mfollow_symlinks)\n\u001b[1;32m    435\u001b[0m copystat(src, dst, follow_symlinks\u001b[39m=\u001b[39mfollow_symlinks)\n\u001b[1;32m    436\u001b[0m \u001b[39mreturn\u001b[39;00m dst\n",
      "File \u001b[0;32m~/anaconda3/envs/pytorch/lib/python3.10/shutil.py:254\u001b[0m, in \u001b[0;36mcopyfile\u001b[0;34m(src, dst, follow_symlinks)\u001b[0m\n\u001b[1;32m    252\u001b[0m     os\u001b[39m.\u001b[39msymlink(os\u001b[39m.\u001b[39mreadlink(src), dst)\n\u001b[1;32m    253\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m--> 254\u001b[0m     \u001b[39mwith\u001b[39;00m \u001b[39mopen\u001b[39;49m(src, \u001b[39m'\u001b[39;49m\u001b[39mrb\u001b[39;49m\u001b[39m'\u001b[39;49m) \u001b[39mas\u001b[39;00m fsrc:\n\u001b[1;32m    255\u001b[0m         \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m    256\u001b[0m             \u001b[39mwith\u001b[39;00m \u001b[39mopen\u001b[39m(dst, \u001b[39m'\u001b[39m\u001b[39mwb\u001b[39m\u001b[39m'\u001b[39m) \u001b[39mas\u001b[39;00m fdst:\n\u001b[1;32m    257\u001b[0m                 \u001b[39m# macOS\u001b[39;00m\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'y_zj_5_lesson01_recv.mp4'"
     ]
    }
   ],
   "source": [
    "import os, re\n",
    "# path = '/Users/earlshaw/Downloads'\n",
    "path = '/Volumes/public/aa/张飞/硬件设计与开发第05部'\n",
    "os.chdir(path)\n",
    "\n",
    "cha = '_5-' # 章节号\n",
    "\n",
    "mp4_files = []\n",
    "for f in os.listdir('.'):\n",
    "    if re.match(r'.*?mp4', f):\n",
    "        # f_a = f.split('.')[0]+'.mp4' #用\n",
    "        # os.system(f\"ffmpeg -i {f} -c copy {f_a}\")\n",
    "        mp4_files.append(f)\n",
    "\n",
    "\n",
    "for f in mp4_files:\n",
    "    if re.match(r'^\\d_', f[13:]): #如何使用正则匹匹配14位字符\n",
    "        # print(f)\n",
    "        g = f[:13]+'0'+f[13:]\n",
    "        # print(g)\n",
    "        mp4_files.remove(f)\n",
    "        mp4_files.append(g)\n",
    "# mp4_files.sort()\n",
    "\n",
    "for f in mp4_files: # 第一次删除会遗留04，08，09，第二次才能删掉，不知为什么\n",
    "    if re.match(r'^\\d_', f[13:]): #如何使用正则匹匹配14位字符\n",
    "        # print(f)\n",
    "        g = f[:13]+'0'+f[13:]\n",
    "        # print(g)\n",
    "        mp4_files.remove(f)\n",
    "        mp4_files.append(g)\n",
    "mp4_files.sort()\n",
    "\n",
    "mp4_files\n",
    "\n",
    "for f in mp4_files:\n",
    "        b = mp4_files.index(f)+1\n",
    "        a = 'lesson'+cha+str(b)+'.mp4'\n",
    "        print(a)\n",
    "        shutil.move(f,a)\n",
    "\n",
    "\n",
    "# for f in mp4_files:\n",
    "#     if f[14] == '_':\n",
    "#         # print(f[13])\n",
    "#         # print('a')\n",
    "#         a = 'lesson'+cha+f[13]+'.mp4'\n",
    "#         shutil.move(f,a)\n",
    "#     else:\n",
    "#         # print(f[13:15])\n",
    "#         # print(f)\n",
    "#         a = 'lesson'+cha+f[13:15]+'.mp4'\n",
    "#         shutil.move(f,a)\n",
    "\n",
    "\n",
    "# 先重命名1-9，然后排序重命名所有\n",
    "\n",
    "\n",
    "\n",
    "# 失败，后续将mp4_files拆为2个list，个位和十位\n",
    "# 个位，十位分别排序重命名，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['233.字符函数.avi',\n",
       " '235.日期时间函数.avi',\n",
       " '201.Navicat下载安装.avi',\n",
       " '248.子查询作为表.avi',\n",
       " '237.多行函数count().avi',\n",
       " '231.查询条件3.avi',\n",
       " '213.自动创建索引.avi',\n",
       " '552_利用Session保存当前登录用户对象.avi',\n",
       " '197.Windows系统安装版MySQL的安装.avi',\n",
       " '246.外连接.avi',\n",
       " '211.外键约束.avi',\n",
       " '220.插入数据1.avi',\n",
       " '556_WebSocket聊天室.avi',\n",
       " '215.创建时创建主键全文复合索引.avi',\n",
       " '245.自连接.avi',\n",
       " '230.条件查询2.avi',\n",
       " '208.非空约束.avi',\n",
       " '524_部署项目(安装配置nginx).avi',\n",
       " '523_部署项目2(多进程访问).avi',\n",
       " '196.MySQL安装文件下载.avi',\n",
       " '232.分页查询.avi',\n",
       " '207.唯一约束.avi',\n",
       " '239.分组统计.avi',\n",
       " '228.查询结果排序.avi',\n",
       " '234.数值函数.avi',\n",
       " '204.创建表.avi',\n",
       " '218.删除索引.avi',\n",
       " '522_部署项目1(单进程访问).avi',\n",
       " '199.MySQL开启和关闭服务.avi',\n",
       " '553_Tornado异步服务器端方式.avi',\n",
       " '203.数据库操作.avi',\n",
       " '195.数据库概述.avi',\n",
       " '551_封装Session工具类.avi',\n",
       " '224.图形化界面操作数据.avi',\n",
       " '216.使用create index创建索引.avi',\n",
       " '217.用alter table创建索引.avi',\n",
       " '209.默认值约束.avi',\n",
       " '221.插入数据2.avi',\n",
       " '227.查询中算术运算符、别名的使用.avi',\n",
       " '247.子查询作为条件.avi',\n",
       " '223.删除数据.avi',\n",
       " '206.主键约束.avi',\n",
       " '210.自动增长约束.avi',\n",
       " '555_WebSocket语法.avi',\n",
       " '243.自然连接.avi',\n",
       " '229.条件查询1.avi',\n",
       " '225.创建emp表并插入数据.avi',\n",
       " '554_Tornado异步客户端.avi',\n",
       " '236.流程函数.avi',\n",
       " '362 BOM对象-location-history-navigator.mp4',\n",
       " '200.MySQL的Windows系统登录和退出.avi',\n",
       " '219.图形化界面操作索引.avi',\n",
       " '212.索引的简介.avi',\n",
       " '244.内连接.avi',\n",
       " '240.having子句.avi',\n",
       " '222.更新数据.avi',\n",
       " '238.多行函数2.avi',\n",
       " '198.Windows平台下ZIP版MySQL的安装.avi',\n",
       " '241.多表查询前创建表.avi',\n",
       " '205.表的查看修改删除操作.avi',\n",
       " '226.基本查询.avi',\n",
       " '242.交叉连接.avi',\n",
       " '202.MySQL的数据类型.avi',\n",
       " '214.创建表时候手动创建普通索引和唯一索引.avi']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os, re, shutil\n",
    "\n",
    "path = '/Volumes/public/aa/python视频/python_3'\n",
    "os.chdir(path)\n",
    "\n",
    "# 列出文件夹内所有pdf，x_t并排序\n",
    "\n",
    "a_files = []\n",
    "\n",
    "for f in os.listdir('.'):\n",
    "    if re.match(r'.*avi', f):\n",
    "        a_files.append(f)\n",
    "\n",
    "a_files\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 文件名写入txt\n",
    "\n",
    "\n",
    "import os, re\n",
    "# path = '/Users/earlshaw/Downloads'\n",
    "# path = '/Users/earlshaw/Downloads/gbc'\n",
    "\n",
    "path = '/Users/earlshaw/Downloads/roms/sfc_rom'\n",
    "re_part = r'.*(bin|sfc)' # 正则 或\n",
    "    #(bin)|(sfc)与(bin|sfc)区别\n",
    "\n",
    "# path = '/Users/earlshaw/Downloads/md_rom'\n",
    "# re_part = r'.*md'\n",
    "\n",
    "# path = '/Users/earlshaw/Downloads/roms/fc'\n",
    "# re_part = r'.*nes'\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "os.chdir(path)\n",
    "\n",
    "a_files = []\n",
    "\n",
    "with open('index.txt', 'w') as f:\n",
    "    for g in os.listdir('.'):\n",
    "        if re.match(re_part, g):\n",
    "            a_files.append(g)\n",
    "    a_files.sort()\n",
    "with open('index.txt', 'w') as f: \n",
    "    for i in range(len(a_files)):\n",
    "        f.writelines(a_files[i]+'\\n')\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "解压目录内的7z，rar，zip等文件\n",
    "'''\n",
    "\n",
    "import os, py7zr, shutil, rarfile\n",
    "\n",
    "\n",
    "# 指定要解压的文件夹路径\n",
    "folder_path = 'path_to_folder'\n",
    "\n",
    "# 确保目标文件夹存在，如果不存在则创建\n",
    "if not os.path.exists(destination_folder):\n",
    "    os.makedirs(destination_folder)\n",
    "\n",
    "# 获取文件夹中所有文件的列表\n",
    "files = os.listdir(folder_path)\n",
    "\n",
    "# 遍历文件夹中的所有文件\n",
    "for file in files:\n",
    "    if file.endswith('.rar'):\n",
    "        # 创建一个RarFile对象\n",
    "        rf = rarfile.RarFile(os.path.join(folder_path, file))\n",
    "        \n",
    "        # 解压文件到指定目录\n",
    "        rf.extractall(path='path_to_extract_folder')\n",
    "        \n",
    "        # 关闭RarFile对象\n",
    "        rf.close()\n",
    "\n",
    "        # 构建源文件路径和目标文件路径\n",
    "        source_file = os.path.join(source_folder, file)\n",
    "        destination_file = os.path.join(destination_folder, file)\n",
    "        # 移动文件\n",
    "        shutil.move(source_file, destination_file)\n",
    "\n",
    "    \n",
    "    \n",
    "    elif file.endswith('.7z'):\n",
    "        # 创建一个Py7zr对象\n",
    "        archive = py7zr.SevenZipFile(os.path.join(folder_path, file), mode='r')\n",
    "        \n",
    "        # 解压文件到指定目录\n",
    "        archive.extractall(path='path_to_extract_folder')\n",
    "        \n",
    "        # 关闭Py7zr对象\n",
    "        archive.close()\n",
    "\n",
    "\n",
    "        source_file = os.path.join(source_folder, file)\n",
    "        destination_file = os.path.join(destination_folder, file)\n",
    "\n",
    "        shutil.move(source_file, destination_file)\n",
    "\n",
    "    elif file.endswith('.zip'):\n",
    "        # 创建一个ZipFile对象\n",
    "        with zipfile.ZipFile(os.path.join(folder_path, file), 'r') as zip_ref: # with...as是什么意思\n",
    "            # 解压文件到指定目录\n",
    "            zip_ref.extractall(path='path_to_extract_folder')\n",
    "\n",
    "        source_file = os.path.join(source_folder, file)\n",
    "        destination_file = os.path.join(destination_folder, file)\n",
    "\n",
    "        shutil.move(source_file, destination_file)\n",
    "\n",
    "print(\"解压完成\")\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/earlshaw/Downloads/漫画/[comicbackup.com][小鬼Q太郎][藤子不二雄A]Vol_06.zip压缩完成\n",
      "/Users/earlshaw/Downloads/漫画/[comicbackup.com][小鬼Q太郎][藤子不二雄A]Vol_01.zip压缩完成\n",
      "/Users/earlshaw/Downloads/漫画/[comicbackup.com][小鬼Q太郎][藤子不二雄A]Vol_04.zip压缩完成\n",
      "/Users/earlshaw/Downloads/漫画/[comicbackup.com][小鬼Q太郎][藤子不二雄A]Vol_03.zip压缩完成\n",
      "/Users/earlshaw/Downloads/漫画/[comicbackup.com][小鬼Q太郎][藤子不二雄A]Vol_02.zip压缩完成\n",
      "/Users/earlshaw/Downloads/漫画/[comicbackup.com][小鬼Q太郎][藤子不二雄A]Vol_05.zip压缩完成\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "目录内文件夹转为zip\n",
    "'''\n",
    "\n",
    "import os, zipfile\n",
    "\n",
    "\n",
    "class FolderZipper:\n",
    "    def __init__(self, folder_path):\n",
    "        self.folder_path = folder_path\n",
    "        \n",
    "\n",
    "\n",
    "    # 输入文件夹名，转为zip\n",
    "    def zip_folder(self, folder_path):\n",
    "\n",
    "        zip_path = f\"{folder_path}.zip\"\n",
    "\n",
    "        with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:\n",
    "            for root, _, files in os.walk(folder_path):\n",
    "\n",
    "                for file in files:\n",
    "                    file_path = os.path.join(root, file)\n",
    "                    archive_name = os.path.relpath(file_path, self.folder_path)\n",
    "                    zipf.write(file_path, archive_name)\n",
    "\n",
    "        print(f'{zip_path}压缩完成')\n",
    "\n",
    "\n",
    "\n",
    "    # 定位并输出要处理的文件夹名\n",
    "    def compress_folders_in_directory(self):\n",
    "        for root, dirs, _ in os.walk(self.folder_path):\n",
    "            for dir_name in dirs:\n",
    "                folder_path = os.path.join(root, dir_name)\n",
    "                # zip_path = f\"{folder_path}.zip\"\n",
    "                self.zip_folder(folder_path)\n",
    "                # print(folder_path)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# 指定要压缩的文件夹所在的目录\n",
    "# directory_path = \"/Users/earlshaw/Downloads/temp\"\n",
    "directory_path = '/Users/earlshaw/Downloads/漫画'\n",
    "\n",
    "# 创建 FolderZipper 实例并压缩文件夹\n",
    "folder_zipper = FolderZipper(directory_path)\n",
    "# folder_zipper.compress_folders_in_directory()\n",
    "folder_zipper.compress_folders_in_directory()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "子文件夹名已写入文档: output.txt\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "将文件夹中的子文件夹名写入文档\n",
    "'''\n",
    "\n",
    "import os\n",
    "\n",
    "# 指定要读取子文件夹名的目标文件夹路径\n",
    "folder_path = '/Volumes/comic/download'\n",
    "\n",
    "# 获取目标文件夹中的子文件夹列表\n",
    "subfolders = [f.name for f in os.scandir(folder_path) if f.is_dir()]\n",
    "\n",
    "# 指定要写入子文件夹名的文档路径\n",
    "output_file = 'output.txt'\n",
    "\n",
    "# 将子文件夹名写入文档\n",
    "with open(output_file, 'w') as file:\n",
    "    for subfolder in subfolders:\n",
    "        file.write(subfolder + '\\n')\n",
    "\n",
    "print('子文件夹名已写入文档:', output_file)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  },
  "vscode": {
   "interpreter": {
    "hash": "3c06e3e46abf38078fe4dac36a0085ec2b134ebbd73dd076183d243eeca6918f"
   }
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
